package com.zznj.xzjc.activity;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;
import androidx.viewpager.widget.ViewPager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.vondear.rxtool.RxActivityTool;
import com.zznj.xzjc.ActivityAnJianDetail;
import com.zznj.xzjc.R;
import com.zznj.xzjc.adapter.index.DsjjsAdapter;
import com.zznj.xzjc.adapter.index.SadwAdapter;
import com.zznj.xzjc.base.BaseActivity;
import com.zznj.xzjc.bean.index.AjcxData;
import com.zznj.xzjc.bean.index.SadwData;
import com.zznj.xzjc.event.EventTag;
import com.zznj.xzjc.event.MessageEvent;
import com.zznj.xzjc.fragment.index.AjcxDsjjsFragment;
import com.zznj.xzjc.net.SharedHelper;
import com.zznj.xzjc.net.entity.BaseBean;
import com.zznj.xzjc.net.entity.DictionaryBean;
import com.zznj.xzjc.net.entity.LoginBean;
import com.zznj.xzjc.net.rxjava.HttpMethods;
import com.zznj.xzjc.net.subscribers.ProgressSubscriber;
import com.zznj.xzjc.net.subscribers.SubscriberOnNextListener;
import com.zznj.xzjc.view.StatusBarUtil;
import com.zznj.xzjc.widget.FragmentAda;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.FormBody;

/**
 * 描述：线索发现
 */
public class ActivityAjcxDsjjs extends BaseActivity {
    Unbinder unbinder;

    @BindView(R.id.searchEdit)
    EditText searchEdit;
    @BindView(R.id.searchBtn)
    ImageView searchBtn;

    @BindView(R.id.srl_all)
    SmartRefreshLayout srlAll;
    @BindView(R.id.rec_main)
    RecyclerView rec_main;
    @BindView(R.id.bottomLinear)
    View bottomLinear;
    @BindView(R.id.tishiImg)
    ImageView tishiImg;

    @Override
    protected int getLayoutId() {
        return R.layout.gyss_activity_ajcx_dsjjs_layout;
    }
    private LoginBean user;

    @Override
    public void initView() {
        user = new Gson().fromJson(SharedHelper.readValueByKeyForString(mContext, "Member"), LoginBean.class);
        setTitle("大数据检索");
        initSrl();
        init();
    }

    @Override
    protected void initData() {
        refresh();
    }

    private List<AjcxData> list;
    private DsjjsAdapter mAdapter;

    private void init() {
        list = new ArrayList<>();
        ((SimpleItemAnimator) rec_main.getItemAnimator()).setSupportsChangeAnimations(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        rec_main.setLayoutManager(linearLayoutManager);
        mAdapter = new DsjjsAdapter(R.layout.gyss_item_dsjjs, list);
        rec_main.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("caseId", list.get(position).getCaseId());
                RxActivityTool.skipActivity(mContext, ActivityAnJianDetail.class, bundle);
            }
        });
    }

    private void initSrl() {
        srlAll.setEnableRefresh(true);
        srlAll.setEnableLoadMore(true);
        srlAll.setEnableAutoLoadMore(true);
        srlAll.setOnRefreshListener(refresh_layout -> {
            refresh();
        });
        srlAll.setOnLoadMoreListener(refresh_layout -> {
            isShowDialog = false;
            page++;
            getData();
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshData(MessageEvent messageEvent) {
        if (messageEvent.getTag().equals(EventTag.DT)) {
            refresh();
        }
    }

    private boolean isShowDialog = true;
    private int page = 0;
    public void refresh() {
        srlAll.setEnableLoadMore(true);
        isShowDialog = false;
        page = 1;
        srlAll.setEnableLoadMore(true);
        list.clear();
        bottomLinear.setVisibility(View.GONE);
//        mAdapter.notifyDataSetChanged();
        getData();
    }

    private void getData() {
        SubscriberOnNextListener<BaseBean<List<AjcxData>>> onNextListener = baseBean -> {
            if (srlAll != null) {
                srlAll.finishLoadMore(true);
                srlAll.finishRefresh(true);
            }
            if (list.size() + baseBean.getResult().size() > 0 && baseBean.getResult().size() < 10) {
                bottomLinear.setVisibility(View.VISIBLE);
                if (srlAll != null) {
                    srlAll.setEnableLoadMore(false);
                }
            }
            list.addAll(baseBean.getResult());
            mAdapter.notifyDataSetChanged();


            if (mAdapter.getItemCount() == 0) {
                tishiImg.setVisibility(View.VISIBLE);
                srlAll.setVisibility(View.GONE);
                tishiImg.setImageResource(R.mipmap.gyss_zwnr_img);
            } else {
                tishiImg.setVisibility(View.GONE);
                srlAll.setVisibility(View.VISIBLE);
            }
        };


        FormBody requestBody = new FormBody.Builder()
                .add("keywords",searchEdit.getText().toString())
                .add("page",page+"")
                .build();
        HttpMethods.getInstance().caseList(new ProgressSubscriber<>(onNextListener, mContext, true), requestBody);
    }

    @OnClick({R.id.searchBtn})
    public void onClick(View v){
        if(v.getId() == R.id.searchBtn){
            refresh();
        }
    }
}
